﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class MyLogin : System.Web.UI.UserControl
{
    private int loaiNguoiDung = 0;

    public int LoaiNguoiDung
    {
        get { return loaiNguoiDung; }
        set { loaiNguoiDung = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        LoaiNguoiDung = loadLogin();
    }

    private int loadLogin()
    {
        HttpCookie tenDangNhap = Request.Cookies["TenDangNhap"];
        HttpCookie matKhau = Request.Cookies["MatKhau"];
        HttpCookie loaiNguoiDung = Request.Cookies["LoaiNguoiDung"];

        string sTenDangNhap = "";
        string sMatKhau = "";
        string sLoaiNguoiDung = "";

        bool isLoginSuccessfully = false;

        if (Session["TenDangNhap"] != null && Session["MatKhau"] != null && Session["LoaiNguoiDung"] != null)
        {
            sTenDangNhap = (string)Session["TenDangNhap"];
            sMatKhau = (string)Session["MatKhau"];
            sLoaiNguoiDung = (string)Session["LoaiNguoiDung"];

            isLoginSuccessfully = true;

        }
        else if ((tenDangNhap != null && matKhau != null && loaiNguoiDung != null))
        {
            sTenDangNhap = (string)tenDangNhap.Value;
            sMatKhau = (string)matKhau.Value;
            sLoaiNguoiDung = (string)loaiNguoiDung.Value;

            if (login(sTenDangNhap, sMatKhau))
            {
                isLoginSuccessfully = true;
            }
        }

        if (isLoginSuccessfully)
        {

            if (sTenDangNhap.Length >= 0 && sMatKhau.Length >= 0)
            {
                // TODO : Thêm hyperlink, khi click vào dang nh?p s? hi?n ra thông tin tài kho?n dó
                pnl_Login.Visible = false;
                pnl_Logout.Visible = true;
                hlUserName.Text = "Chào: " + sTenDangNhap;
                hlUserName.NavigateUrl = @"~\Default2.aspx?TenDangNhap=" + sTenDangNhap;
            }

            // TODO : Xử lý theo loại người dùng.
            switch (sLoaiNguoiDung)
            {

                case "1":
                    {
                        return 1;
                        break;
                    }
                case "2":
                    {
                        return 2;
                        break;
                    }
                case "3":
                    {
                        return 3;
                        break;
                    }
                case "4":
                    {
                        return 4;
                        break;
                    }
                case "5":
                    {
                        return 5;
                        break;
                    }
                default:
                    {
                        return 0;
                        break;
                    }
            }
        }
        else
        {
            pnl_Login.Visible = true;
            pnl_Logout.Visible = false;
            return 0;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string tenDangNhap = Login1.UserName.Trim();

        string matKhau = Login1.Password.ToString();

        clickloginbutton(tenDangNhap, matKhau);
    }

    private bool clickloginbutton(string tenDangNhap, string matKhau)
    {
        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();

        var query = from Nguoi_Dung in linQ.NGUOI_DUNGs where Nguoi_Dung.TenDangNhap == tenDangNhap select Nguoi_Dung;

        foreach (NGUOI_DUNG nd in query)
        {
            if (nd.MatKhau.Trim() == matKhau && nd.Deleted == false)
            {
                HttpCookie tendangnhap = new HttpCookie("TenDangNhap");
                tendangnhap.Value = nd.TenDangNhap;
                HttpCookie matkhau = new HttpCookie("MatKhau");
                matkhau.Value = nd.MatKhau;
                HttpCookie loaiNguoiDung = new HttpCookie("LoaiNguoiDung");
                loaiNguoiDung.Value = nd.MaLoaiNguoiDung.ToString();

                // TODO: Thêm hành d?ng sau khi dang nh?pt hành công
                if (Login1.RememberMeSet)
                {
                    Response.Cookies.Add(tendangnhap);
                    Response.Cookies.Add(matkhau);
                    Response.Cookies.Add(loaiNguoiDung);
                }
                else
                {
                    Session.Add("TenDangNhap", nd.TenDangNhap);
                    Session.Add("MatKhau", nd.MatKhau);
                    Session.Add("LoaiNguoiDung", nd.MaLoaiNguoiDung.ToString());
                }

                //TODO: C?n chuy?n d?n trang theo lo?i tài kho?n.
                Response.Redirect(@"~\index.aspx");
                return true;
                // Page_Load(sender, e);

            }
            else
            {
                //TODO: Neu tai khoan bi sai
                Login1.FailureText = " Mật khẩu hoặc tên đăng nhậpp không dúng";
            }
            // Page_Load(sender, e);

            // TODO : CHo qua trang sai mật khẩu
            Response.Redirect(@"~\WrongPassword.aspx");

            return false;
        }

        // TODO : Thêm hành dông nu? dang nh?p không thành công.
        return false;
    }

    protected void btn_Logout_Click(object sender, EventArgs e)
    {
        Response.Cookies["TenDangNhap"].Expires = DateTime.Now.AddDays(-1);
        Response.Cookies["MatKhau"].Expires = DateTime.Now.AddDays(-1);
        Response.Cookies["LoaiNguoiDung"].Expires = DateTime.Now.AddDays(-1);

        if (Session["TenDangNhap"] != null)
        {
            Session.Remove("TenDangNhap");
        }
        if (Session["TenDangNhap"] != null)
        {
            Session.Remove("TenDangNhap");
        }
        if (Session["TenDangNhap"] != null)
        {
            Session.Remove("TenDangNhap");
        }

        Response.Redirect("index.aspx");
    }

    protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
    {
        Button1_Click(sender, e);
    }

    private bool login(string tenDangNhap, string matKhau)
    {
        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();

        var query = from Nguoi_Dung in linQ.NGUOI_DUNGs where Nguoi_Dung.TenDangNhap == tenDangNhap select Nguoi_Dung;

        foreach (NGUOI_DUNG nd in query)
        {
            if (nd.MatKhau.Trim() == matKhau && nd.Deleted == false)
            {
                HttpCookie tendangnhap = new HttpCookie("TenDangNhap");
                tendangnhap.Value = nd.TenDangNhap;
                HttpCookie matkhau = new HttpCookie("MatKhau");
                matkhau.Value = nd.MatKhau;
                HttpCookie loaiNguoiDung = new HttpCookie("LoaiNguoiDung");
                loaiNguoiDung.Value = nd.MaLoaiNguoiDung.ToString();

                return true;
                // Page_Load(sender, e);
            }
            else
            {

            }
            // Page_Load(sender, e);

            return false;
        }
        return false;
    }
}
